package com.ng.framework.view.desktop.table;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class NGColumnGroup {

	private TableCellRenderer renderer;
	private List<Object> groupContent; //TODO: pasar a dos listas diferentes (una para TableColumn y otra para NGColumnGroup.
	private String text;
	private int margin;

	public NGColumnGroup(String text) {
		this(null, text);
	}

	public NGColumnGroup(TableCellRenderer renderer, String text) {
		if (renderer == null) {
			this.renderer = new DefaultTableCellRenderer() {

				private static final long serialVersionUID = -79425963204779170L;

				public Component getTableCellRendererComponent(JTable table,
						Object value, boolean isSelected, boolean hasFocus,
						int row, int column) {

					JTableHeader header = table.getTableHeader();
					if (header != null) {
						this.setForeground(header.getForeground());
						this.setBackground(header.getBackground());
						this.setFont(header.getFont());
					}

					this.setHorizontalAlignment(JLabel.CENTER);
					this.setText((value == null) ? "" : value.toString());
					this.setBorder(UIManager
							.getBorder("TableHeader.cellBorder"));

					return this;
				}
			};
		} else {
			this.renderer = renderer;
		}
		this.text = text;
		this.groupContent = new ArrayList<Object>();
	}

	public void add(TableColumn tableColumn) {
		groupContent.add(tableColumn);
	}

	public void add(NGColumnGroup columnGroup) {
		groupContent.add(columnGroup);
	}

	public List<Object> getColumnGroups(TableColumn tableColumn, List<Object> g) {
		g.add(this);

		if (!groupContent.contains(tableColumn)) {
			for (Object obj : groupContent) {
				if (obj instanceof NGColumnGroup) {
					List<Object> groups = ((NGColumnGroup) obj)
							.getColumnGroups(tableColumn,
									new ArrayList<Object>(g));
					if (groups != null) {
						return groups;
					}
				}
			}
			return new ArrayList<Object>();

		} else {
			return g;
		}
	}

	public TableCellRenderer getHeaderRenderer() {
		return renderer;
	}

	public void setHeaderRenderer(TableCellRenderer renderer) {
		if (renderer != null) {
			this.renderer = renderer;
		}
	}

	public Object getHeaderValue() {
		return text;
	}

	public Dimension getSize(JTable table) {
		Component comp = renderer.getTableCellRendererComponent(table,
				getHeaderValue(), false, false, -1, -1);
		int height = comp.getPreferredSize().height;
		int width = 0;

		for (Object obj : groupContent) {
			if (obj instanceof TableColumn) {
				TableColumn aColumn = (TableColumn) obj;
				width += aColumn.getWidth();
				width += margin;
			} else {
				width += ((NGColumnGroup) obj).getSize(table).width;
			}
		}

		return new Dimension(width, height);
	}

	public void setColumnMargin(int margin) {
		this.margin = margin;

		for (Object obj : groupContent) {
			if (obj instanceof NGColumnGroup) {
				((NGColumnGroup) obj).setColumnMargin(margin);
			}
		}

	}
}
